{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-01T06:05:53.041879Z",
     "start_time": "2025-09-01T06:05:52.968101Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数学和统计方法",
   "id": "386f297c62a5b024"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 1. 常用方法演示\n",
    "![](images/img.png)"
   ],
   "id": "ea2da243b24a08fd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T06:23:21.645227Z",
     "start_time": "2025-09-01T06:23:21.632627Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.arange(1,10)\n",
    "print(f'原数组是:{arr1}')\n",
    "print(f'元素累加之和:{np.sum(arr1)}')\n",
    "print(f'数组元素的平均值:{np.mean(arr1)}')\n",
    "print(f'数组元素的额标准差是:{np.std(arr1)}')\n",
    "print(f'方差是“{np.var(arr1)}')\n",
    "print(f'最大值:{np.max(arr1)}，其索引是:{np.argmax(arr1)}')\n",
    "print(f'最小值：{np.min(arr1)},其索引是:{np.argmin(arr1)}')\n",
    "print(f'中位数:{np.median(arr1)}')\n",
    "# 25 百分位数（25th Percentile）就是将数据从小到大排列后，处于 25% 位置的值。\n",
    "print(f'百分位数:{np.percentile(arr1,25)}')\n",
    "# 0.5 分位数（0.5 Quantile）就是将数据从小到大排列后，处于 1/2 位置的值。\n",
    "print(f'分位数:{np.quantile(arr1,0.5)}')\n",
    "print(f'累计和：{np.cumsum(arr1)}')\n",
    "print(f'累计积:{np.cumprod(arr1)}')\n",
    "print(f'加权平均数:{np.average(arr1)}')\n"
   ],
   "id": "6b6e660b7d57add0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组是:[1 2 3 4 5 6 7 8 9]\n",
      "元素累加之和:45\n",
      "数组元素的平均值:5.0\n",
      "数组元素的额标准差是:2.581988897471611\n",
      "方差是“6.666666666666667\n",
      "最大值:9，其索引是:8\n",
      "最小值：1,其索引是:0\n",
      "中位数:5.0\n",
      "百分位数:3.0\n",
      "分位数:5.0\n",
      "累计和：[ 1  3  6 10 15 21 28 36 45]\n",
      "累计积:[     1      2      6     24    120    720   5040  40320 362880]\n",
      "加权平均数:5.0\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 平均数，加权平均数，中位数，众数",
   "id": "bc128fa38f9cac0e"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 2.1 众数\n",
    "- 在Numpy中无法直接表示众数，可以通过以下方式获取"
   ],
   "id": "c7ef94c7d2ce950c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T07:16:23.022908Z",
     "start_time": "2025-09-01T07:16:23.013771Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 方法1：使用bincount（仅适用于非负整数）\n",
    "arr2 = np.array([1,2,3,4,4,2,5,8,4,6,8,7,4,7,7,7,7])\n",
    "print(arr2)\n",
    "counts = np.bincount(arr2)\n",
    "print(counts)\n",
    "# 找出counts中的最大值\n",
    "mode = np.argmax(counts)\n",
    "print(mode)\n",
    "\n",
    "print('=' * 30)\n"
   ],
   "id": "8f79e1d32b95f5d8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 4 2 5 8 4 6 8 7 4 7 7 7 7]\n",
      "[0 1 2 1 4 1 1 5 2]\n",
      "7\n",
      "==============================\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T07:32:53.602508Z",
     "start_time": "2025-09-01T07:32:53.598515Z"
    }
   },
   "cell_type": "code",
   "source": [
    "values = np.array([1, 2, 3, 4])\n",
    "weights = np.array([0.1, 0.2, 0.3, 0.4])\n",
    "\n",
    "# aa = (values * weights)\n",
    "# print(np.sum(aa) / np.sum(weights))\n",
    "ax = np.average(values, weights=weights)\n",
    "print(ax)"
   ],
   "id": "c0967f061f5d26ea",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n"
     ]
    }
   ],
   "execution_count": 44
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
